[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
scanf() Read Formatted Data from Stdin
#include <stdio.h>
int scanf(format-string[,argument,...]);
const char *format-string; Format control
scanf() reads data one character at a time from 'stdin' (the standard
input stream) and stores it in the locations given by 'arguments'.
'format-string' determines how the input fields are to be
interpreted. Each argument must be a pointer to a variable with a
type that corresponds to a type specifier in 'format-string'.
'format-string' is a character string that contains whitespace
characters, non-whitespace characters, and format specifications.
Here is a description of the arguments of scanf.
format-string: The format string is read from left to right. When
the first format specification is encountered, the
value of the first input field is converted according
to the format specification, and the converted value
is then stored in the location specified by the first
argument. The value of the second input field is
converted according to the second format
specification and stored in the second location, and
so on. Characters outside the format string--
whitespace characters and non-whitespace characters,
described below--should match the sequence of
characters being read from the input stream.
Whitespace characters:
Are the blank (' '), tab ('\t'), and newline ('\n')
characters. The scanf() function reads consecutive
whitespace characters up to the next non-whitespace
character in the input, but does not store them. One
whitespace character in the format-string matches any
number and combination of whitespace characters in
the input.
Non-whitespace characters:
Are all remaining ASCII characters other than the
percent character (%). The scanf() function will
read but not store a matching non-whitespace
character. If the next character scanned does not
match, the function will terminate.
Format specifications:
Are introduced by the percent sign (%). Format
specifications cause the scanf() function to read and
convert characters from the input field into specific
types of values. These values are assigned to
arguments in the argument list. A format
specification has the following form:
%[*][width][{F|N}][{h|l}]type
Type: The type character, which appears after the last
optional format field, determines whether the input
field is interpreted as a character, a string, or a
number. The simplest format specification contains
only the percent sign and a type character (%s, for
example). The various type specifications are:
Character Input Expected Type of Argument
-----------------------------
d decimal integer Pointer to int
D decimal integer Pointer to long
o octal integer Pointer to int
O octal integer Pointer to long
x hexadecimal integer Pointer to int
X Hexadecimal integer Pointer to long
i decimal, hex, or octal integer Pointer to int
I decimal, hex, or octal integer Pointer to long
u unsigned decimal integer Pointer to unsigned int
U unsigned decimal integer Pointer to unsigned long
f floating-point value Pointer to float
c character Pointer to char
s string Pointer to char array
n no input read Pointer to int
p value in the form of xxxx:yyyy, Pointer to far data item
where x and y are
uppercase hexadecimal
digits
To read strings not delimited by space characters, a set of
characters in brackets ([]) can be substituted for the 's' string
type. To store strings without storing a terminating null character
('\0') use the specification '%nc', where 'n' is a decimal integer.
Asterisk: The asterisk (*) character following the percent sign
suppresses assignment of the next input field. The
type character following the * is assumed to specify
the type of suppressed input data. The field is
scanned but not stored.
Width: The width is a positive decimal integer and controls
the maximum number of characters that will be read
from the current input field. No more than 'width'
characters are converted and stored at the
corresponding argument.
F and N: Override the default or declared size of 'argument'.
'F' should be prefixed to an argument pointing to a
'far' object. 'N' should be prefixed to an argument
pointing to a 'near' object.
h and l: Indicate which version of the following 'type' is to
be used.
The prefix 'l' indicates the 'long' version is to be
used. The corresponding argument should point to a
'long' or 'double' object. The 'l' modifier can be
used with the d, i, o, u, x, e, and f type
characters.
The prefix 'h' indicates the short version is to be
used. The corresponding argument should point to a
'short' object. The 'h' modifier can be used with d,
i, o, u, and x type characters.
'l' and 'h' modifiers are ignored if used with any
other type.
Returns: The number of fields that were successfully converted
and assigned. A return value of EOF means an attempt
was made to read at end-of-file. A return value of 0
means no fields were assigned.
Notes: scanf() may stop reading a particular input field
before it reaches a space character because:
o the specified width has been reached;
o the next character cannot be converted as specified;
o the next character conflicts with a character in the
control string;
o the next character fails to appear in the character
set, or does appear in an inverted character set;
When any of these situations occur, the next input
field is considered to begin at the first unread
character.
-------------------------------- Example ---------------------------------
The following statements write a prompt to the screen, then read the
data entered from the keyboard.
#include <stdio.h>
int num;
char name[15];
main()
{
do
{
printf("Enter employee number and last name
(0 0 to exit):");
scanf("%4d%15s",&num,name);
if (num != 0)
printf("%s (employee number: %d) entered.\n",name,num);
} while (num != 0);
}
See Also:
fscanf()
printf()
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson